package cn.kgc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.BCrypt;
import cn.kgc.entity.Admin;
import cn.kgc.form.AdminAddForm;
import cn.kgc.form.RestPwdForm;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.interceptor.JwtInterceptor;
import cn.kgc.service.AdminService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Api(tags = "招聘后台用户管理模块")
@Validated
@Slf4j
@CrossOrigin
@SuppressWarnings("all")
public class AdminController {
    @Resource
    private AdminService adminService;

    @PostMapping("/restPwd")
    @ApiOperation(value="重置密码")
    public GraceJSONResult resetPwd(@RequestBody RestPwdForm form){
        //获得当前的用户ID
        String s = JwtInterceptor.adminUserIds.get();
        //验证密码是否正确
        Admin admin = adminService.getById(s);
        if(BCrypt.checkpw(form.getOldPwd(),admin.getPassword())){
            //如果密码正确,修改用户的密码
            admin.setPassword(BCrypt.hashpw(form.getNewPwd()));
            return GraceJSONResult.ok(adminService.updateById(admin));
        }
        //如果密码错误
        return GraceJSONResult.errorMsg("密码错误");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户信息")
    public GraceJSONResult delete(String id){
        if(StrUtil.isBlank(id)){
            return GraceJSONResult.errorMsg("用户id不能为空");
        }
        if(!checkIsAdmin()){
            return GraceJSONResult.errorMsg("权限不足");
        }
        String adminId = JwtInterceptor.adminUserIds.get();
        if(adminId.equals(id)){
            return GraceJSONResult.errorMsg("不能删除当前登录用户");
        }
        return GraceJSONResult.ok(adminService.removeById(id));
    }

    @ApiOperation(value = "新增用户信息")
    @PostMapping("/save")
    public GraceJSONResult  save(@RequestBody AdminAddForm form){
        log.debug("新增管理员用户:{}",form);
        if(!checkIsAdmin()){
            return GraceJSONResult.errorMsg("权限不足");
        }
        String username = form.getUsername();
        if(checkUserNameRepeat(username)){
            return GraceJSONResult.errorMsg("用户名重复");
        }
        Admin admin = new Admin();
        BeanUtil.copyProperties(form,admin);
        admin.setPassword(BCrypt.hashpw(admin.getPassword()));  //密码加密
        return GraceJSONResult.ok(adminService.save(admin));
    }
    /**
     * 验证当前登录用户是否是管理员
     */
    private boolean checkIsAdmin(){
        String adminId = JwtInterceptor.adminUserIds.get();
        if(adminId!=null){
            Admin admin = adminService.getById(adminId);
            if(admin!=null && admin.getUsername().equals("admin")){
                return true;
            }
        }
        return false;
    }

    private boolean checkUserNameRepeat(String username){
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername,username);
        return  adminService.count(queryWrapper)>0;
    }
}
